package com.prosay.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 专门用来动态显示聊天信息的 servlet
 * @author Administrator
 *
 */
//@WebServlet("/ShowMsgServlet")
public class ShowMsgServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public void service(HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		// 获取消息列表
		ServletContext application = this.getServletContext();
		List<String> msgs = (List<String>)application.getAttribute("msgs");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<meta charset=\"UTF-8\">");
		
		// 设置成每隔一秒钟 自动刷新一次
		out.println("<meta http-equiv='refresh' content='1'>");

		out.println("</head>");
		out.println("<body>");
		if(msgs!=null){
			for(String msg:msgs){
				out.println(msg);
				out.println("<br>");
			}
		}
			
		out.println("</body>");
		out.println("</html>");
		
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
